<template>
 <div class="staff"><div>
            <el-form
                :inline="true"
                ref="search_data" 
                :model='search_data' >
                   

                <el-form-item> 
                  <div class="search">
                        <el-input v-model="input" placeholder="请输入内容" style="width:400px"></el-input>
                    <el-button type="primary" size ="small"  icon="el-icon-search" style="margin-left: 10px"  @click='onScreeoutMoney()'>搜索</el-button>
                  </div>
              
                </el-form-item>
                 <el-form-item class="btnRight">
                    <el-button  v-if="userInfo" type="primary" size ="small" icon="view" @click='onAddMoney()'>添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="table_container">
            <el-table
                v-if="tableData.length > 0"
                :data='tableData'
                max-height="450"
                border
                :default-sort = "{prop: 'date', order: 'descending'}"
                style="width: 100%">
                 <el-table-column
                    prop="name"
                    label="供应商"
                    align='center'
                    width="200">
                </el-table-column>
                <el-table-column
                    prop="orderName"
                    label="供应物品"
                    align='center'
                    width="200"
                    sortable>
  
                </el-table-column>
                <el-table-column
                    prop="describe"
                    label="货物类型"
                    align='center'
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="price"
                    label="商品价格"
                    align='center'
                    width="100">
                </el-table-column>
                <el-table-column
                    prop="head"
                    label="负责人"
                    align='center'
                    width="170">                    
                </el-table-column>

                <el-table-column
                    prop="phoneNumber"
                    label="联系电话"
                    align='center'
                    width="170">                    
                </el-table-column>
              
                <el-table-column
                    prop="operation"
                    align='center'
                    label="操作"
                    v-if="userInfo"
                    width="180">
                    <template slot-scope='scope'>
                        <el-button 
                            type="warning" 
                            icon='edit' 
                            size="small"
                            @click='onEditstaff(scope.row)'
                        >编辑</el-button>
                        <el-button 
                            type="danger" 
                            icon='delete' 
                            size="small"
                            @click='onDeleteMoney(scope.row,scope.$index)'
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
             <el-row>
                <el-col :span="24">
                    <div class="pagination">
                        <el-pagination
                            v-if='paginations.total > 0'
                            :page-sizes="paginations.page_sizes"
                            :page-size="paginations.page_size"
                            :layout="paginations.layout"
                            :total="paginations.total"
                            :current-page.sync='paginations.page_index'
                            @current-change='handleCurrentChange'
                            @size-change='handleSizeChange'>
                        </el-pagination>
                    </div>
                </el-col>
            </el-row>
        </div>
        <!-- 弹框页面 -->
        <DialogSupplier :dialog='dialog' :form='form' @update="getProfile"></DialogSupplier></div>
</template>

<script>
import DialogSupplier from '../components/DialogSupplier.vue'
export default {
  name: 'staff',
  data() {
    return {
      input: '',
      tableData: [],
      allTableData: [],
      filterTableData: [],
      dialog: {
        show: false,
        title: '',
        option: 'edit',
      },
      form: {
        type: '',
        describe: '',
        income: '',
        expend: '',
        cash: '',
        remark: '',
        id: '',
      },
      //需要给分页组件传的信息
      paginations: {
        page_index: 1, // 当前位于哪页
        total: 0, // 总数
        page_size: 5, // 1页显示多少条
        page_sizes: [5, 10, 15, 20], //每页显示多少条
        layout: 'total, sizes, prev, pager, next, jumper', // 翻页属性
      },
      search_data: {
        startTime: '',
        endTime: '',
      },
    }
  },
  components: {
    DialogSupplier,
  },
  created() {
    this.getProfile(), this.getuser()
  },

  methods: {
    getuser() {
      let userinfo = this.$store.getters.user.identity
      if (userinfo == 'manager') {
        userinfo = true
      } else {
        userinfo = false
      }
      this.userInfo = userinfo
    },
    getProfile() {
      // 获取表格数据
      this.$axios('/api/supplier/').then((res) => {
        // this.tableData = res.data;
        this.allTableData = res.data
        this.filterTableData = res.data
        // 设置分页数据
        this.setPaginations()
      })
    },
    onEditstaff(row) {
      // 编辑
      this.dialog = {
        show: true,
        title: '修改员工信息',
        option: 'edit',
      }
      this.form = {
        name: row.name,
        orderName: row.orderName,
        describe: row.describe,
        price: row.price,
        head: row.head,
        phoneNumber: row.phoneNumber,
        id: row._id,
      }
    },
    onDeleteMoney(row, index) {
      // 删除
      this.$axios.delete(`/api/supplier/delete/${row._id}`).then((res) => {
        this.$message('删除成功')
        this.getProfile()
      })
    },
    onAddMoney() {
      // 添加
      this.dialog = {
        show: true,
        title: '添加供应商信息',
        option: 'add',
      }
      this.form = {
        name: '',
        describe: '',
        orderName: '',
        price: '',
        head: '',
        phoneNumber: '',
        id: '',
      }
    },
    handleCurrentChange(page) {
      // 当前页
      let sortnum = this.paginations.page_size * (page - 1)
      let table = this.allTableData.filter((item, index) => {
        return index >= sortnum
      })
      // 设置默认分页数据
      this.tableData = table.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },
    handleSizeChange(page_size) {
      // 切换size
      this.paginations.page_index = 1
      this.paginations.page_size = page_size
      this.tableData = this.allTableData.filter((item, index) => {
        return index < page_size
      })
    },
    setPaginations() {
      // 总页数
      this.paginations.total = this.allTableData.length
      this.paginations.page_index = 1
      this.paginations.page_size = 10
      // 设置默认分页数据
      this.tableData = this.allTableData.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },
    onScreeoutMoney() {
      // console.log(this.input)
      if (this.input == '') {
        // console.log('1555555555222')
        this.getProfile()
      } else
        this.$axios.get(`api/supplier/getinfo/${this.input}`).then((res) => {
          // console.log(res.data)
          // console.log('1231313222222')

          let arr = []
          arr[0] = res.data
          this.tableData = arr
        })
    },
  },
}
</script>

<style scoped>
.fillcontain {
  width: 100%;
  height: 100%;
  padding: 16px;
  box-sizing: border-box;
}
.search {
  display: block;
  float: left;
  margin-left: 30px;
  margin-top: 10px;
  margin-right: 50px;
}
.btnRight {
  margin-left: 450px;
  margin-top: 10px;
}
.tj {
  text-decoration: none;
  color: rgb(255, 255, 255);
}
.pagination {
  text-align: left;
  margin-top: 30px;
  margin-left: 500px;
}
</style>